function generateTree(){
    $.ajax({
        "url": "/menu/do/get.json",
        "type": "post",
        "datatype": "json",
        "success": function (response) {
            let result = response.result
            if (result == null) {

                // 创建json对象用于控制树形结构
                let setting = {
                    "view": {
                        "addDiyDom": myAddDiyDom,
                        "addHoverDom": myAddHoverDom,
                        "removeHoverDom": myRemoveHoverDom
                    },
                    "data": {
                        "key": {
                            "url": "notExist"
                        }
                    }
                };

                let zNodes = response.data;
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            }
            if (result == "FAILED") {
                layer.msg(response.message);
            }
        }
    });
}

function myAddDiyDom(treeId, treeNode) {

    // 获取生成节点的序号来改变样式
    let spanId = treeNode.tId + "_ico"

    $("#" + spanId).removeClass().addClass(treeNode.icon);

}

function myAddHoverDom(treeId, treeNode) {

    let btnGroupId = treeNode.tId + "_btnGrp";

    if ($("#" + btnGroupId).length > 0) {
        return;
    }

    let addBtn = "<a id='" + treeNode.id + "' class='addBtn btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' href='#' title='增加节点'>&nbsp;&nbsp;<i class='fa fa-fw fa-plus rbg '></i></a>";
    let editBtn = "<a id='" + treeNode.id + "' class='editBtn btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' href='#' title='修改节点'>&nbsp;&nbsp;<i class='fa fa-fw fa-edit rbg '></i></a>";
    let removeBtn = "<a id='" + treeNode.id + "' class='removeBtn btn btn-info dropdown-toggle btn-xs' style='margin-left:10px;padding-top:0px;' href='#' title='删除节点'>&nbsp;&nbsp;<i class='fa fa-fw fa-times rbg '></i></a>";

    // 获取当前节点级别
    let level = treeNode.level;
    let btn = ""

    if (level === 0) {
        btn = addBtn;
    }

    if (level === 1) {
        btn = addBtn+ " " +editBtn;
        let length = treeNode.children.length;
        if (length === 0){
            btn = btn + " " +removeBtn
        }
    }

    if (level === 2){
        btn = editBtn + " " + removeBtn;
    }

    let anchorId = treeNode.tId + "_a";

    $("#" + anchorId).after("<span id='" + btnGroupId + "'>btn</span>");

}

function myRemoveHoverDom(treeId, treeNode) {

    let btnGroupId = treeNode.tId + "_btnGrp";

    $("#" + btnGroupId).remove();

}